import 'package:flutter/material.dart';
import 'package:flutter_learn_demo/notifier/counter.dart';

class ChangeNotifierTest extends StatefulWidget {

  const ChangeNotifierTest({super.key});

  @override
  State<StatefulWidget> createState() => ChangeNotifierTestState();

}

class ChangeNotifierTestState extends State<ChangeNotifierTest> {

  int value = 0;

  @override
  void initState() {
    super.initState();
    //添加事件监听
    counter.addListener(() {
        setState(() {
           value = counter.count;
        });
    });
  }

  @override
  Widget build(BuildContext context) {
     return Scaffold(
       appBar: AppBar(
         title: const Text('ChangeNotifierTest'),
         backgroundColor: Colors.teal,
       ),
       body: Center(
         child: ElevatedButton(
           onPressed: () {
              counter.addCount();
           },
           child: Text('当前值:$value'),
         ),
       ),
     );
  }
}

Counter counter = Counter();